import pty
import os
import select
import time
import threading


class gpsSimulator():
    def __init__(self):
        # 打开伪终端
        self.noSpeed = {
            "gga": b"$GNGGA,030409.00,2308.09149,N,11320.35677,E,2,11,1.08,32.9,M,-5.4,M,1.0,3357*42\n",
            "rmc": b"$GNRMC,030410.00,A,2308.09165,N,11320.35698,E,0.048,,080821,,,F,V*18\n",
        }

        self.hasSpeed = {
            "gga": b"$GNGGA,030409.00,2308.09149,N,11320.35677,E,2,11,1.08,32.9,M,-5.4,M,1.0,3357*42\n",
            "rmc": b"$GNRMC,030410.00,A,2308.09165,N,11320.35698,E,100.048,,080821,,,F,V*19\n",
        }

        self.delta4 = {
            "gga": b"$GNGGA,030409.00,2308.05149,N,11320.30677,E,2,11,1.08,32.9,M,-5.4,M,1.0,3357*4b\n",
            "rmc": b"$GNRMC,030410.00,A,2308.05165,N,11320.30698,E,9.048,,080821,,,F,V*18\n",
        }

        self.nowGps = self.noSpeed

        self.stop_atuo = False
        self.calibrationFlag = False

        master1, slave = os.openpty()
        slaveName1 = os.ttyname(slave)
        print('gps simlate: ' + slaveName1)
        self.master = master1
        self.slave = slave

    def write_gps(self):
        master = self.master
        while True:
            time.sleep(1)
            os.write(master, self.nowGps["gga"])
            os.write(master, self.nowGps["rmc"])

    def read_qx(self):
        while True:
            rl, wl, el = select.select([self.slave], [], [], 1)
            for device in rl:
                data = os.read(device, 128)
                print("read %d data: %s" % (len(data), data))

    def set_now_gps(self, type):
        if type == "1":
            self.nowGps = self.noSpeed
            self.stop_atuo = True
            self.calibrationFlag = False
        if type == "2":
            self.nowGps = self.hasSpeed
            self.stop_atuo = True
            self.calibrationFlag = False
        if type == "3":
            self.nowGps = self.delta4
            self.stop_atuo = True
            self.calibrationFlag = False
        if type == "999":
            t1 = threading.Thread(target=gps.calibration)
            self.stop_atuo = False
            self.calibrationFlag = True
            t1.start()
        if type == "1000":
            t2 = threading.Thread(target=gps.auto_switch)
            self.stop_atuo = False
            self.calibrationFlag = False
            t2.start()

    def auto_switch(self):
        while self.stop_atuo == False:
            time.sleep(3)

            if self.stop_atuo == True:
                break
            self.nowGps = self.hasSpeed
            time.sleep(20)

            if self.stop_atuo == True:
                break
            self.nowGps = self.noSpeed
            time.sleep(60)

            for i in range(10):
                if self.stop_atuo == True:
                    break
                self.nowGps = self.delta4
                time.sleep(1)

                if self.stop_atuo == True:
                    break
                self.nowGps = self.hasSpeed
                time.sleep(1)

            if self.stop_atuo == True:
                break
            self.nowGps = self.noSpeed
            time.sleep(60)

    def calibration(self):
        for i in range(10000):
            if self.calibrationFlag == False:
                break
            self.nowGps = self.delta4
            time.sleep(1)

            if self.calibrationFlag == False:
                break
            self.nowGps = self.hasSpeed
            time.sleep(1)

    def get_user_input(self):
        while True:
            cmd = input()
            self.set_now_gps(cmd)


if __name__ == "__main__":
    gps = gpsSimulator()
    t1 = threading.Thread(target=gps.write_gps)
    t2 = threading.Thread(target=gps.read_qx)
    input_thread = threading.Thread(target=gps.get_user_input)
    t1.start()
    input_thread.start()
    # t2.start()
